<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">

      <el-form-item label="姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="投诉类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择投诉类型" clearable class="!w-240px">
          <el-option v-for="dict in option" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['wine:complain-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <!-- <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wine:complain-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="主键ID" align="center" prop="id" />-->
      <!-- <el-table-column label="信息类型: Complain_Type表主键ID" align="center" prop="typeId" /> -->
      <!-- <el-table-column label="信息名称" align="center" prop="infoName" /> -->
      <!-- <el-table-column label="描述" align="center" prop="description" /> -->
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" /> -->
      <el-table-column label="用户昵称" align="center" prop="nickname" width="100px" />
      <el-table-column label="发布用户" align="center" prop="name" width="100px" />
      <el-table-column label="被投诉" align="center" prop="realName" width="100px" />
      <el-table-column label="手 机" align="center" prop="mobile" width="120px" />
      <el-table-column label="区 域" align="center" prop="areaName" width="200px" />
      <el-table-column label="地 址" align="center" prop="address" />
      <el-table-column label="公 司" align="center" prop="company" width="220px" />
      <el-table-column label="审核状态" align="center" prop="audit" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WINE_CHECK" :value="scope.row.audit" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="置顶" align="center" prop="isTop" width="80px">
        <template #default="scope">
          <el-switch :value="scope.row.isTop" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否"
            inline-prompt @click="handleStatusChange(scope.row)" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" width="160px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm(scope.row.id)">
            详情
          </el-button>
          <el-button link type="primary" v-if="scope.row.audit == 0" @click="openDetail(scope.row)"
            v-hasPermi="['wine:complain-info:update']">
            审核
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:complain-info:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <Dialog v-model="dialogVisible" title="审核" top="50" align-center width="500" @close=resetForm>
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="审核" prop="audit">
          <el-select placeholder="请选择" v-model="formData.audit" class="w-80">
            <el-option v-for="dict in getIntDictOptions(DICT_TYPE.WINE_CHECK)" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="auditClick">
            确定
          </el-button>
        </div>
      </template>
    </Dialog>
  </ContentWrap>

</template>

<script setup lang="ts">
// import { dateFormatter } from '@/utils/formatTime'
import { ComplainInfoApi, ComplainInfoVO } from '@/api/wine/complaininfo'
import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";

/** 投诉信息 列表 */
defineOptions({ name: 'ComplainInfo' })
const { push } = useRouter() // 路由跳转

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ComplainInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  typeId: undefined,
  infoName: undefined,
  realName: undefined,
  mobile: undefined,
  areaId: undefined,
  areaName: undefined,
  address: undefined,
  company: undefined,
  image: undefined,
  images: undefined,
  description: undefined,
  isTop: undefined,
  sort: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

const option = ref<ListItem[]>([])

/** 初始化 **/
onMounted(async () => {
  await getList()
  await getComptypeList()
})

interface ListItem {
  value: string
  label: string
}
const getComptypeList = async () => {
  const data = await ComplainInfoApi.getComptypeList()
  console.log("data", data);
  option.value = []
  data.map((item => {
    option.value.push({
      label: item.typeName,
      value: item.id
    })
  }))
}
const formRef = ref() // 表单 Ref

const formData = ref({
  audit: undefined
})

const rules = reactive({
  audit: [{ required: true, message: '请选择审核', trigger: 'change' }],
})

const resetForm = () => {
  formData.value = {
    audit: undefined
  }
  formRef.value?.resetFields()
}
const auditLoading = ref(false)

const auditClick = async () => {
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  auditLoading.value = true

  auditRow.value.audit = formData.value.audit
  try {
    await ComplainInfoApi.updateComplainInfo(auditRow.value)
    message.success('操作成功')
    getList()
    auditLoading.value = false
    dialogVisible.value = false
  } finally {
    auditLoading.value = false
    dialogVisible.value = false
  }
}
const dialogVisible = ref(false)
const auditRow = ref()
const openDetail = (row: object) => {
  console.log(row)
  auditRow.value = row
  dialogVisible.value = true
}


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ComplainInfoApi.getComplainInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const openForm = (id?: number) => {
  push({ name: 'ComplaininfoDetail', params: { id } })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ComplainInfoApi.deleteComplainInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}



const handleStatusChange = async (row: any) => {
  try {
    let data = row
    // 二次确认
    const text = data.isTop == 1 ? '取消置顶' : '置顶'
    await message.confirm(`确认要${text}"${row.infoName}"吗？`).then(async (res) => {
      console.log("err", res);
      data.isTop = data.isTop == 1 ? 0 : 1
      // 发起修改
      await ComplainInfoApi.updateComplainInfo(data)
      message.success(text + '成功')
      // 刷新列表
      await getList()
    }).catch((err) => {
      console.log("err", err);
    })
  } catch {
    // 异常时，需要重置回之前的值
    await getList()
  }
}


</script>
